using System.Collections;
using UnityEngine;

public class ThumbController : MonoBehaviour
{
	public UISprite controllerBase;

	public UISprite thumb;

	private bool hidden;

	private bool initialized;

	private void Init()
	{
		if (!initialized)
		{
			initialized = true;
			base.gameObject.SetActive(false);
			hidden = true;
		}
	}

	private void Awake()
	{
		Init();
	}

	public void Hide()
	{
		Init();
		if (!hidden)
		{
			hidden = true;
			Fade(1f, 0f, 0.2f, true);
		}
	}

	public void ShowOnPosition(Vector3 firstTouchScreenPos, Vector3 thumbScreenPos)
	{
		Init();
		Camera mainCamera = UICamera.mainCamera;
		Vector3 position = mainCamera.ScreenToWorldPoint(firstTouchScreenPos);
		Vector3 position2 = mainCamera.ScreenToWorldPoint(thumbScreenPos);
		controllerBase.cachedTransform.position = position;
		thumb.cachedTransform.position = position2;
		if (hidden)
		{
			hidden = false;
			base.gameObject.SetActive(true);
			Fade(0f, 1f, 0.2f, false);
		}
	}

	protected void Fade(float from, float to, float duration, bool deactivate)
	{
		StopAllCoroutines();
		StartCoroutine(DoFade(from, to, duration, deactivate));
	}

	protected IEnumerator DoFade(float from, float to, float duration, bool deactivate)
	{
		float time = 0f;
		while (time < duration)
		{
			time += RealTime.deltaTime;
			float normTime = time / duration;
			controllerBase.alpha = Mathf.Lerp(from, to, normTime);
			yield return null;
		}
		controllerBase.alpha = to;
		if (deactivate)
		{
			base.gameObject.SetActive(false);
		}
	}
}
